package com.base;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.constant.Constant;
import com.ddd.wandemo.R;
import com.event.PageEvent;
import com.trello.rxlifecycle2.LifecycleTransformer;
import com.util.NetUtils;
import com.widget.PageLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public abstract class BaseRefreshFragment<T extends BaseRefreshContract.Presenter> extends BaseFragment implements BaseRefreshContract.View {
    private boolean isViewCreated;
    private boolean isUIVisible;
    public T mPresenter;
    public PageLayout view;
    private PageLayout mPageLayout;
    private Handler mHandler = new Handler();
    public boolean delayLoad = true;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = initPageLayout();
        view.changePageState(PageLayout.LOADING);
        return view;
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        isViewCreated = true;
        mPresenter = initPresenter();
        if (mPresenter != null) {
            mPresenter.attachView(this);
        }
        checkNet();
    }

    //bind lifecycle
    @Override
    public <T> LifecycleTransformer<T> bindToLife() {
        return bindToLifecycle();
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null)
            mPresenter.detachView();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    protected PageLayout initPageLayout() {
        mPageLayout = new PageLayout(mContext);
        return mPageLayout
                .setNoDataPage(R.layout.activity_base_no_data)
                .setNoNetPage(R.layout.activity_base_no_net)
                .setLoading(R.layout.activity_base_loading)
                .setError(R.layout.activity_base_error)
                .setNormalPage(getLayoutId())
                .build();
    }

    private void checkNet() {
        mPageLayout.setPageLayoutClickListener(new PageLayout.PageLayoutClickListener() {
            @Override
            public void onReloadClick() {
                mPageLayout.changePageState(PageLayout.LOADING);
                //重置lazyload
                isViewCreated = true;
                isUIVisible = true;
                checkNet();
            }
        });
        if (NetUtils.isConnected(mContext)) {
            Constant.CONN = true;
            lazyLoad();
            delayLoad();
        } else {
            onNoNet();
            Constant.CONN = false;
        }
    }

    private void delayLoad() {
        if (delayLoad) {
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    view.changePageState(PageLayout.NORMAL);
                }
            }, 1000);
        }else {
            view.changePageState(PageLayout.NORMAL);
        }
    }

    private void lazyLoad() {
        if (isViewCreated && isUIVisible) {
            loadData();
            //数据加载完毕,恢复标记,防止重复加载
            isViewCreated = false;
            isUIVisible = false;
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            isUIVisible = true;
            lazyLoad();
        } else {
            isUIVisible = false;
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handlePageState(PageEvent pageEvent) {
        int code = pageEvent.getState();
        switch (code) {
            case PageLayout.ERROR:
                mPageLayout.changePageState(code);
                mPageLayout.setErrorMsg(pageEvent.getMsg());
                break;
            case PageLayout.NO_NET:
                mPageLayout.changePageState(code);
                mPageLayout.setNoNetMsg(pageEvent.getMsg());
                break;
            default:
                break;
        }

    }


    @Override
    public void onNoNet() {
        view.changePageState(PageLayout.NO_NET);
    }

    @Override
    public void onNoData() {
        view.changePageState(PageLayout.NO_DATA);
    }

    protected abstract T initPresenter();

}
